/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DijagnozaNewPanel.java
 *
 * Created on 20.10.2011., 19:39:45
 */
package hr.fer.zpr.nmbp.project2.presentation;

import hr.fer.zpr.nmbp.project2.model.Bolest;
import hr.fer.zpr.nmbp.project2.model.Dijagnoza;
import hr.fer.zpr.nmbp.project2.model.Pacijent;
import hr.fer.zpr.nmbp.project2.presentation.util.MyComboBoxModel;
import hr.fer.zpr.nmbp.project2.presentation.util.MyListModel;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 *
 * @author Miki
 */
public class DijagnozaNewPanel extends javax.swing.JPanel {

    /** Creates new form DijagnozaNewPanel */
    public DijagnozaNewPanel() {
        initComponents();
        afterInit();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jXDatePicker1 = new org.jdesktop.swingx.JXDatePicker();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextArea1 = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jComboBox1 = new javax.swing.JComboBox();

        setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Dijagnoza", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 11))); // NOI18N

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("Pacijent");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Datum");

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText("Opis");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel4.setText("Bolesti");

        jTextArea1.setColumns(20);
        jTextArea1.setLineWrap(true);
        jTextArea1.setRows(5);
        jTextArea1.setWrapStyleWord(true);
        jScrollPane1.setViewportView(jTextArea1);

        jScrollPane2.setViewportView(jList1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
                    .addComponent(jXDatePicker1, javax.swing.GroupLayout.DEFAULT_SIZE, 241, Short.MAX_VALUE)
                    .addComponent(jComboBox1, 0, 241, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jXDatePicker1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 73, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel4)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea jTextArea1;
    private org.jdesktop.swingx.JXDatePicker jXDatePicker1;
    // End of variables declaration//GEN-END:variables

    private MyComboBoxModel<Pacijent> pacijentiModel;
    private MyListModel<Bolest> bolestiModel;
    
    private String sifra = null;
    
    private void afterInit() {
        pacijentiModel = new MyComboBoxModel<Pacijent>();
        jComboBox1.setModel(pacijentiModel);
        bolestiModel = new MyListModel<Bolest>();
        jList1.setModel(bolestiModel);
    }
    
    public void reset(List<Pacijent> pacijenti, List<Bolest> bolesti) {
        sifra = null;
        
        jComboBox1.setEnabled(true);
        pacijentiModel.setList(pacijenti);
        jXDatePicker1.setDate(Calendar.getInstance().getTime());
        jTextArea1.setText("");
        bolestiModel.setList(bolesti);
        jList1.setSelectedIndices(new int[] {});
    }

    public Dijagnoza getDijagnoza() {
        Dijagnoza dijagnoza = new Dijagnoza();
        dijagnoza.setSifra(sifra);
        
        dijagnoza.setKarton(((Pacijent) jComboBox1.getSelectedItem()).getKarton());
        dijagnoza.setDatum(jXDatePicker1.getDate());
        dijagnoza.setOpis(jTextArea1.getText().trim());
        
        Object[] selected = jList1.getSelectedValues();
        List<Bolest> bolesti = new ArrayList<Bolest>();
        for (Object s : selected) {
            bolesti.add((Bolest) s);
        }
        dijagnoza.setBolesti(bolesti);
        
        return dijagnoza;
    }

    void setDijagnoza(Dijagnoza dijagnoza, List<Pacijent> pacijenti, List<Bolest> bolesti) {
        sifra = dijagnoza.getSifra();
        pacijentiModel.setList(pacijenti);
        jComboBox1.setSelectedItem(dijagnoza.getKarton().getPacijent());
        jComboBox1.setEnabled(false);
        
        jXDatePicker1.setDate(dijagnoza.getDatum());
        jTextArea1.setText(dijagnoza.getOpis());
        bolestiModel.setList(bolesti);
        
        int len = dijagnoza.getBolesti().size();
        int[] indicies = new int[len];
        for (int i = 0; i < len; i++) {
            indicies[i] = bolesti.indexOf(dijagnoza.getBolesti().get(i));
        }
        jList1.setSelectedIndices(indicies);
    }
    
}
